<template>
    <div>
        <div class="goods-list">
            <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="goDetail(item.id)">
                <img :src="item.img_url" alt="">
                <h1 class="title" v-text="item.title"></h1>
                <div class="info">
                    <p class="price">
                        <span class="new">¥{{item.sell_price}}</span>
                        <span class="old"><del>¥{{item.market_price}}</del></span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span>
                        <span>剩余{{item.stock_quantity}}件</span>
                    </p>
                </div>
            </div>

            <div class="goods-item">
                <img src="../../images/meimei.jpg" alt="">
                <h1 class="title">小米（Mi）小米Note 16G双网通版米（Mi）小米Note 16G双网通版</h1>
                <div class="info">
                    <p class="price">
                        <span class="new">¥2199</span>
                        <span class="old"><del>¥2399</del></span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span>
                        <span>剩余60件</span>
                    </p>
                </div>
            </div>

            <div class="goods-item">
                <img src="../../images/meimei.jpg" alt="">
                <h1 class="title">小米（Mi）小米Note 16G双网通版</h1>
                <div class="info">
                    <p class="price">
                        <span class="new">¥2199</span>
                        <span class="old"><del>¥2399</del></span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span>
                        <span>剩余60件</span>
                    </p>
                </div>
            </div>
        </div>
        <!-- 1.绑定事件 -->
        <!-- 2.在事件处理函数中，让 pageindex+1 -->
        <!-- 3.调用现成的 getAllGoodsList方法 获取下一页的数据-->
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    export default{
        data(){
            return{
                pageindex:1,//页码
                goodslist:[]//存放商品的数组
            }
        },
        created(){
            this.goodslist();
        },
        methods:{
            getAllGoodsList(){
                //获取商品的数据列表
                this.$http.get('api/getgoods?pageindex=n'+ this.pageindex).then(result=>{
                    if(result.body.status === 0){
                        // this.goodslist = result.body.message;
                        this.goodslist = this.goodslist.concat(result.body.message);
                    }
                })
            },
            getMore(){
                //点击加载更多
                this.pageindex++;
                this.getAllGoodsList();
            },
            toDetail(id){
                //点击，使用编程式导航跳转到 详情页面
                // console.log(this);

                //this.$route 是路由参数对象
                
                //路由导航对象 this.$router
                //通过调用 this.$router.push()方法，采用JS形式跳转到详情页面
                //如果值传递了一个参数，那么这个参数，就是要去的那个URL地址
                this.$router.push('/home/goodsinfo/'+id);
            }
        }
    }
</script>
<style lang="scss" scoped>
.goods-list{
    display:flex;
    flex-wrap: wrap;
    padding:6px;
    justify-content: space-between;
    .goods-item{
        width:49%;
        border:1px solid #ccc;
        box-shadow: 0 0 6px #ccc;
        margin:3px 0;
        padding: 2px;
        // 解决商品底部空白
        display:flex;
        flex-direction: column;
        justify-content: space-between;
        img{
            width:100%;
        }
        .title{
            font-size:14px;
        }

        .info{
            background-color:#eee;
            p{
                margin:0;
                padding:5px 0;
            }
            .price{

                .old{
                    font-size: 14px;
                    font-style: italic;
                    margin-left:10px;
                }
                .new{
                    color:red;
                    font-weight: bold;
                    font-size: 16px;
                }
            }
            .sell{
                display:flex;
                justify-content: space-between;
                font-size: 13px;
            }
        }
    }
}
</style>
